package com.stiove.whwh.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stiove.whwh.dao.mapper.TicketHotMapper;
import com.stiove.whwh.dao.pojo.TicketHot;
import com.stiove.whwh.vo.TicketVo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ThreadService {
    @Async("taskExecutor")
    public void updateViewCount(TicketHotMapper ticketHotMapper, TicketVo ticketVo) {

        TicketHot ticketHot = new TicketHot();
        ticketHot.setHot(ticketVo.getHot()+1);
        LambdaQueryWrapper<TicketHot> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TicketHot::getTicketId, ticketVo.getId());

        if (ticketHotMapper.selectOne(queryWrapper) == null){
            ticketHot.setTicketId(ticketVo.getId());
            ticketHotMapper.insert(ticketHot);
        }
        queryWrapper.eq(TicketHot::getHot, ticketVo.getHot());
        ticketHotMapper.update(ticketHot, queryWrapper);
    }

}
